﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr3.ST;
using System.IO;

namespace com.guysherman.simulclass
{
    public class ScannedField : ScannedMember
    {
        private string GetterMethodName
        {
            get
            {
                return string.Format("{0}{1}_{2}", nameDecoratorForVisibility(this.Visibility), "get", this.Name);
            }
        }

        private string SetterMethodName
        {
            get
            {
                return string.Format("{0}{1}_{2}", nameDecoratorForVisibility(this.Visibility), "set", this.Name);
            }
        }

        public override string MemberFunctionDeclaration()
        {
            StringBuilder declaration = new StringBuilder();

            StringTemplateGroup group = new StringTemplateGroup(new StringReader(Templates.HeaderTemplate));
            StringTemplate t = group.GetInstanceOf("memberFunctionDeclaration");

            List<string> args = new List<string>();
            args.Add("MonoObject* target");


            t.SetAttribute("decorators",  this.MemberType.EmbeddedTypeName());
            t.SetAttribute("name", GetterMethodName);
            t.SetAttribute("args", args);
            declaration.AppendLine(t.ToString());
            t.Reset();

            t.SetAttribute("decorators",  "void");
            t.SetAttribute("name", SetterMethodName);
            args.Add(string.Format("{0} {1}", this.MemberType.EmbeddedTypeName(), this.Name));
            t.SetAttribute("args", args);
            declaration.AppendLine(t.ToString());

            return declaration.ToString();
        }

        

        

       

        

        public override string MemberVariableDeclaration()
        {
            return string.Format("MonoClassField* {0};", this.Name); 
        }



        public override string MemberFunctionDefinition()
        {
            StringBuilder definition = new StringBuilder();

            StringTemplateGroup group = new StringTemplateGroup(new StringReader(Templates.HeaderTemplate));

            string templateName = string.Empty;
            if (this.MemberType.FullName == "System.String")
            {
                templateName = "stringFieldGetterSetter";
            }
            else if (this.MemberType.IsPrimitive)
            {
                templateName = "valueFieldGetterSetter";
            }
            else
            {
                templateName = "refFieldGetterSetter";
            }
            
            StringTemplate t = group.GetInstanceOf(templateName);

            t.SetAttribute("className", this.ClassName);
            t.SetAttribute("getterMethodName", GetterMethodName);
            t.SetAttribute("setterMethodName", SetterMethodName);
            t.SetAttribute("type", this.MemberType.EmbeddedTypeName());
            t.SetAttribute("memberName", this.Name);
 
            definition.AppendLine(t.ToString());

            return definition.ToString();
        }
    }
}
